﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Clipper.Model;
using System.Collections.ObjectModel;
using Clipper.BLL.DetectChain;
using Global;
using System.ComponentModel;
using System.Runtime.InteropServices;
using Clipper.ClipperWindow;
using Clipper.EngineerWindow;
using System.Threading;
using Communication;

namespace Clipper.ClipperUserControl
{
    /// <summary>
    /// UserEngineer.xaml 的交互逻辑
    /// </summary>
    public partial class UserEngineer : UserControl
    {
        ReactionStrip mStrip = null;

        public UserEngineer()
        {
            InitializeComponent();
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            mStrip = new ReactionStrip()
            {
                ReactionCupList=new ObservableCollection<ReactionCup>()
            };
            
        }
        //总复位
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            DeliveryWarehouse.Reset();
            AddReagent1AndSample.Reset();
            AddReagent2.Reset();
            AddSubstrate.Reset();
            IncubationBox.BedControl.WaitStatus(IncubationBox.BedControl.Reset());
            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.Reset());
            Transportation.Reset();
            //Transportation.Clear();

            //btnDetect.IsEnabled = true;
        }

        private void btnDeliveryWare_Click(object sender, RoutedEventArgs e)
        {

            //btnDeliveryWare.IsEnabled = false;
            //DeliveryWarehouse deliver = new DeliveryWarehouse();
            //deliver.RunEngineer(new ReactionStrip());
            //btnDeliveryWare.IsEnabled = true;
        }
        private void btnDeliverReset_Click(object sender, RoutedEventArgs e)
        {
            DeliveryWarehouse.Reset();
        }

        private void btnAddR1AddSample_Click(object sender, RoutedEventArgs e)
        {


            //置样本试剂1 为加完状态
            ServiceProvider.CupStrip[Convert.ToInt32(StripID.Text)].Cups[Convert.ToInt32(CupId.Text)].setStatus(Model.DetectionModel.Cup.CupHoleStatusEnum.SAMPLE);



            //for (int i = 0; i < 8; i++)
            //{
            //    ReactionCup cup = new ReactionCup();
            //    cup.DetectItem = "FT3";
            //    mStrip.ReactionCupList.Add(cup);
            //    //CurveFitting.CurveContext ff = new CurveFitting.CurveContext();
                
            //}
            //while (true)
            //{
            //    AddReagent1AndSample.RunEngineer(mStrip);
            //}
        }

        private void btnR1Reset_Click(object sender, RoutedEventArgs e)
        {
            AddReagent1AndSample.Reset();
        }

        private void btnTransportReset_Click(object sender, RoutedEventArgs e)
        {
            Transportation.Reset();
        }

        private void btnDetect_Click(object sender, RoutedEventArgs e)
        {
            //DeliveryWarehouse.Reset();
            //AddReagent1AndSample.Reset();
            //AddReagent2.Reset();
            //AddSubstrate.Reset();
            //Transportation.Reset();
            //DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.Reset());
            //IncubationBox.IncubationControl.WaitStatus(IncubationBox.IncubationControl.Begin());//孵育升温
          
           Simulate();
        }
        //构造虚拟的反应条，走正常的流程
        private void Simulate()
        {
            ServiceProvider.gReactionStripList = new ObservableCollection<ReactionStrip>();//模拟添加条
            Random random = new Random();

            for (int i = 0; i <100; i++)
            {
                byte incubate = byte.Parse(random.Next(20, 40).ToString());

                ReactionStrip strip = new ReactionStrip()
                {
                    ID = i,
                    WhileIncubator = 20,
                    WhileLightReact = 7,
                    ReactionCupList=new ObservableCollection<ReactionCup>(),
                };
                for (int j = 0; j < 7; j++)
                {
                    ReactionCup cup = new ReactionCup();
                    cup.DetectItem = "TSH";
                    DetectItems item = new DetectItems();
                    item.detectItem = "TSH";
                    item.reagentAmount1 = 10;
                    item.reagentAmount2 = 10;
                    cup.DetectItemModel = item;
                    strip.ReactionCupList.Add(cup);
                }
                if (i<50)
                {
                    strip.DetectionMethod = DetectionMethodEnum.ONE_STEP_METHOD;
                }
                else
                {
                    strip.DetectionMethod = DetectionMethodEnum.TWO_STEP_METHOD;
                }

                ServiceProvider.gReactionStripList.Add(strip);
            }
            DetectChainHelper.InitDetectChain();
            DetectChainHelper.RunDetectChain();
        }

        private void btnInTransport_Click(object sender, RoutedEventArgs e)
        {
            //R1到R2
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
            //Transportation.TransportA.GoIncubation();
            //Transportation.TransportA.WaitStatus(3);
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
            //Transportation.TransportA.Home();
            //Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
            //Transportation.TransportB.WaitStatus(Transportation.TransportB.Home());
            //Transportation.TransportB.LeftPosition();
            //Transportation.TransportA.WaitStatus(0);
            //Transportation.TransportB.WaitStatus(1);

            //R2到孵育
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
            //Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
            //Transportation.TransportB.WaitStatus(Transportation.TransportB.Home());
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(16));

            //到清洗2
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash_1(2));
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash(2));

            //到检测偏
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.GetStrip(60));
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect_1());
            Transportation.TransportA.LeftPosition();


        }

        private void btnR2_Click(object sender, RoutedEventArgs e)
        {
            //置样本试剂1 为加完状态
            ServiceProvider.CupStrip[Convert.ToInt32(StripID.Text)].Cups[Convert.ToInt32(CupId.Text)].setStatus(Model.DetectionModel.Cup.CupHoleStatusEnum.REAGENT1);
            //AddReagent2.RunEngineer(new ReactionStrip());
        }

        private void btnR2Reset_Click(object sender, RoutedEventArgs e)
        {
            AddReagent2.Reset();
        }

        private void btnGetStrip_Click(object sender, RoutedEventArgs e)
        {
            IncubationBox.RunEngineer(new ReactionStrip());
        }

        //洗板
        private void btnWash_Click(object sender, RoutedEventArgs e)
        {

            #region 调试
            //Transportation.TransportD.GoIncubation();
            //Transportation.TransportD.WaitStatus(3);
            //Transportation.TransportD.Position();
            //Transportation.TransportD.WaitStatus(1);
            //WashingTank.Wash1.Wash();
            //WashingTank.Wash1.WaitStatus(1);
            //Transportation.TransportD.GoIncubation();
            //Transportation.TransportD.WaitStatus(3);

            //Transportation.TransportE.GoIncubation();
            //Transportation.TransportE.WaitStatus(3);
            //Transportation.TransportE.Position();
            //Transportation.TransportE.WaitStatus(1);
            //WashingTank.Wash2.Wash();
            //WashingTank.Wash2.WaitStatus(1);
            //Transportation.TransportE.GoIncubation();
            //Transportation.TransportE.WaitStatus(3);

            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.MoveLittle());
            //for (int i = 0; i < 2; i++)
            //{
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());
            //    WashingTank.Wash1.WashIn();
            //    Thread.Sleep(8000);
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //    WashingTank.Wash1.WashOut();
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());
            //}
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.MoveLittle());
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //WashingTank.Wash1.WashOut();
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashUp());

            //WashingTank.Wash2.WaitStatus(WashingTank.Wash2.MoveLittle());
            //for (int i = 0; i < 2; i++)
            //{
            //    WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown_1());
            //    WashingTank.Wash2.WashIn();
            //    Thread.Sleep(8000);
            //    WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());
            //    WashingTank.Wash2.WashOut();
            //    WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown_1());
            //}
            //WashingTank.Wash2.WaitStatus(WashingTank.Wash2.MoveLittle());
            //WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());
            //WashingTank.Wash2.WashOut();
            //WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashUp());
            #endregion

            #region 洗板1
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.GetStrip(int.Parse( txtGetPosition.Text)));
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash_1(1));
            //Transportation.TransportD.WaitStatus(Transportation.TransportD.GoIncubation());
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash(1));
            //Transportation.TransportD.WaitStatus(Transportation.TransportD.WashPosition());

            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //WashingTank.Wash1.WashOut();

            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.MoveLittle());
            //for (int i = 0; i < 8; i++)
            //{
            //    //Wash1.WashIn();
            //    //Wash1.WaitStatus(Wash1.WashDown());
            //    //Wash1.WashOut();
            //    //Wash1.WaitStatus(Wash1.WashDown_1());

            //    WashingTank.Wash1.WashIn();
            //    Thread.Sleep(5000);
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //    WashingTank.Wash1.WashOut();
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());

            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.MoveLittle());
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //    WashingTank.Wash1.WashOut();
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.MoveLittle());

            //}

            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashUp());

            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash(1));
            //Transportation.TransportD.WaitStatus(Transportation.TransportD.GoIncubation());
            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash_1(1));
            //Transportation.TransportD.WaitStatus(Transportation.TransportD.Home());
            #endregion

            #region 洗板2

            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.GetStrip(int.Parse("")));
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash_1(2));
            Transportation.TransportE.WaitStatus(Transportation.TransportE.GoIncubation());
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash(2));
            Transportation.TransportE.WaitStatus(Transportation.TransportE.WashPosition());

            //WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());
            //WashingTank.Wash2.WashOut(4000);
            //WashingTank.Wash2.WashIn(4000);
            //WashingTank.Wash2.WashOut(4000);

            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.MoveLittle());
            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());
            WashingTank.Wash2.WashOut(4000);
            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown_1());
            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.MoveLittle());
            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());
            WashingTank.Wash2.WashOut(4000);

            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown_1());
            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.MoveLittle());
            for (int i = 0; i < 5; i++)
            {
                //Wash1.WashIn();
                //Wash1.WaitStatus(Wash1.WashDown());
                //Wash1.WashOut();
                //Wash1.WaitStatus(Wash1.WashDown_1());

                WashingTank.Wash2.WashIn(2000);
                Thread.Sleep(15000);
                WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());
                WashingTank.Wash2.WashOut(4000);
                WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown_1());

                WashingTank.Wash2.WaitStatus(WashingTank.Wash2.MoveLittle());
                WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());
                WashingTank.Wash2.WashOut(4000);
                WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown_1());
                WashingTank.Wash2.WaitStatus(WashingTank.Wash2.MoveLittle());

            }

            WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashUp());

            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash(2));
            Transportation.TransportE.WaitStatus(Transportation.TransportE.GoIncubation());
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash_1(2));
            Transportation.TransportE.WaitStatus(Transportation.TransportE.Home());
            #endregion


            TranAddSubstrate();

            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(1));

        }

        private void btnWashReset_Click(object sender, RoutedEventArgs e)
        {
            //Transportation.TransportD.Home();
            Transportation.TransportE.Home(); 
        }

        private void btnSubstrate_Click(object sender, RoutedEventArgs e)
        {
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.GetStrip(1));
            //AddSubstrate.Engineer();
            TranAddSubstrate();

            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(1));
         
        }

        private void btnSubstrateReset_Click(object sender, RoutedEventArgs e)
        {
            AddSubstrate.Reset();
        }

        private void btnDetectHouse_Click(object sender, RoutedEventArgs e)
        {
            DetectionHouse.RunEngineer();
        }

        private void btnDetectHouseReset_Click(object sender, RoutedEventArgs e)
        {
            DetectionHouse.DetectControl.Reset();
        }

        private void btnTransportB_Click(object sender, RoutedEventArgs e)
        {
            //Transportation.TransportB.GoIncubation();
            Transportation.Reset();
            BackgroundWorker backWorker = new BackgroundWorker();
            backWorker.DoWork += (ss, ee) =>
                {
                    int i = 1;
                    while (true)
                    {
                        Console.WriteLine("--------{0}开始---------", i);
                        //DeliveryWarehouse.DeliveryControl.WaitStatus(DeliveryWarehouse.DeliveryControl.CupIn());
                        //DeliveryWarehouse.DeliveryControl.WaitStatus(DeliveryWarehouse.DeliveryControl.Back2000());

                        //R1到R2
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                        //Transportation.TransportA.GoIncubation();
                        //Transportation.TransportA.WaitStatus(3);
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1_1());
                        //Transportation.TransportA.Home();
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                        //Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
                        //Transportation.TransportB.WaitStatus(Transportation.TransportB.Home());
                        //IncubationBox.BedControl.Rock();

                        //Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                        //Transportation.TransportB.WaitStatus(Transportation.TransportB.Home());
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1_1());
                        //Transportation.TransportA.GoIncubation();
                        //Transportation.TransportA.WaitStatus(3);
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                        //Transportation.TransportA.WaitStatus(Transportation.TransportA.Home());
                        //IncubationBox.BedControl.Rock();
                        
                        //滑轨B
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                        //Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
                        //Transportation.TransportB.WaitStatus(Transportation.TransportB.Home());
                        //Transportation.TransportB.WaitStatus(Transportation.TransportB.RightPosition());

                        //滑轨D
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash(1));
                        //Transportation.TransportD.WaitStatus(Transportation.TransportD.GoIncubation());
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash_1(1));
                        //Transportation.TransportD.WaitStatus(Transportation.TransportD.WashPosition());
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Substrate());

                        //小车测试
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Wash(2));
                        //Thread.Sleep(1000);
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect_1());
                        //Thread.Sleep(1000);
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect());
                        //Thread.Sleep(1000);
                        //IncubationBox.BedControl.Rock();
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                        //Thread.Sleep(100);
                        //Transportation.IncubationTransport.R1_1();
                        //Transportation.IncubationTransport.Detect_1();
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                        //Thread.Sleep(100);
                        //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
                        //Thread.Sleep(100);
                        //IncubationBox.BedControl.Rock();

                        i++;
                    }
                };
            backWorker.RunWorkerAsync();
        }

        private void btnCarReset_Click(object sender, RoutedEventArgs e)
        {
            Transportation.IncubationTransport.IncubationReset();
            Transportation.IncubationTransport.WaitStatus(0);
        }

        private void btnCx_Click(object sender, RoutedEventArgs e)
        {
            //IncubationBox.IncubationControl.WaitStatus(IncubationBox.IncubationControl.Begin());
            //IncubationBox.IncubationControl.WaitStatus(IncubationBox.IncubationControl.Stop());
            //IncubationBox.IncubationControl.WaitStatus(IncubationBox.IncubationControl.TemperatureSet(3700));
            IncubationBox.IncubationControl.GetTemperature();
            //byte i = 0x0a;
            //byte j = 0xad;
            //MessageBox.Show((i * 0x100 + j).ToString());
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            AddReagent1AndSample.Pause(true);
            AddReagent2.Pause(true);
            AddSubstrate.Pause(true);
            IncubationBox.Pause(true);
            DeliveryWarehouse.Pause(true);
            Transportation.Pause(true);
            WashingTank.Pause(true);
            
        }

        private void btnResume_Click(object sender, RoutedEventArgs e)
        {
            AddReagent1AndSample.Pause(false);
            AddReagent2.Pause(false);
            AddSubstrate.Pause(false);
            IncubationBox.Pause(false);
            DeliveryWarehouse.Pause(false);
            Transportation.Pause(false);
            WashingTank.Pause(false);
        }

        private void btnLoopSample_Click(object sender, RoutedEventArgs e)
        {
            AddReagent1AndSample.Sample.WaitStatus(AddReagent1AndSample.Sample.Reset());
            BackgroundWorker back = new BackgroundWorker();
            back.DoWork += (ss, ee) =>
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        AddReagent1AndSample.Sample.WaitStatus(AddReagent1AndSample.Sample.Position());
                        AddReagent1AndSample.Sample.WaitStatus(AddReagent1AndSample.Sample.ChangeShelf());
                    }
                };
            back.RunWorkerAsync();
        }
        private void btnLightValue_Click(object sender, RoutedEventArgs e)
        {
            //DetectionHouse detect = new DetectionHouse();
            //ReactionStrip strip = new ReactionStrip() { DetectEndTime = DateTime.Now };
            //strip.ReactionCupList = new ObservableCollection<ReactionCup>();
            //for (int i = 0; i < 8; i++)
            //{
            //    strip.ReactionCupList.Add(new ReactionCup() { 
            //        LightValue=1 ,
            //        DetectItem="TSH",
            //        SampleInfoID=i,
            //    });
            //}
            //detect.SaveDetectResult(strip);

        }
        private void btnAdvance_Click(object sender, RoutedEventArgs e)
        {
            WinEngineer win = new WinEngineer();
            win.ShowDialog();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //ServiceProvider.Monitor.ClipperState = ComponentStateEnum.ERROR;
            //ServiceProvider.Monitor.USBConnectState = ComponentStateEnum.ERROR;
            //DetectChainHelper.ReSend();
            //AddReagent1AndSample.Sample.GetBarcode();
            //AddReagent1AndSample.Sample.SV(3000);

        }

        private void btnOpen_Click(object sender, RoutedEventArgs e)
        {
            AddReagent1AndSample.SA.MainOpen();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            AddReagent1AndSample.SA.MainClose();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            #region 临时
            //for (int i = 0; i < 80; i++)
            //{
            //    //Transportation.TransportD.WaitStatus(Transportation.TransportD.Home());
            //    //for (int j = 0; j < 5; j++)
            //    //{
            //    //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());
            //    //    Thread.Sleep(1000);
            //    //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //    //    Thread.Sleep(1000);
            //    //}
            //    //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashUp());
            //    //Thread.Sleep(1000);

            //    //R2A循环
            //    AddReagent2.R2A.ControlStatus = ControlStatusEnum.BUSY;
            //    AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Imbibe());
            //    AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Inject());
            //    AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Rinse());
            //    AddReagent2.R2A.WaitRinseTime();
            //    while (AddReagent2.R2A.ControlStatus != ControlStatusEnum.IDLE)
            //    {
            //        //Console.WriteLine("cccc");
            //    }
            //}
            #endregion
             BackgroundWorker worker = new BackgroundWorker();
             worker.DoWork += (ss, ee) =>
                 {
                     for (int j = 0; j < 10; j++)
                     {
                         Transportation.TransportA.WaitStatus(Transportation.TransportA.Home());
                         DeliveryWarehouse.DeliveryControl.WaitStatus(DeliveryWarehouse.DeliveryControl.CupIn());
                         Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                         Transportation.TransportA.WaitStatus(Transportation.TransportA.GoIncubation());
                         DeliveryWarehouse.DeliveryControl.Back2000();

                         Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                         Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
                         Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2());
                         Transportation.TransportB.WaitStatus(Transportation.TransportB.Home());

                         for (int i = 0; i < 8; i++)
                         {
                             if (i < 4)
                             {
                                 Transportation.TransportB.WaitStatus(Transportation.TransportB.LeftPosition());
                                 while (AddReagent2.R2A.ControlStatus != ControlStatusEnum.IDLE)
                                 {
                                     //Console.WriteLine("cccc");
                                 }
                                 AddReagent2.R2A.ControlStatus = ControlStatusEnum.BUSY;
                                 AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Imbibe());
                                 AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Inject());
                                 AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Rinse());
                                 AddReagent2.R2A.WaitRinseTime();

                             }
                             else
                             {
                                 Transportation.TransportB.WaitStatus(Transportation.TransportB.RightPosition());
                                 while (AddReagent2.R2A.ControlStatus != ControlStatusEnum.IDLE)
                                 {
                                     //Console.WriteLine("cccc");
                                 }
                                 AddReagent2.R2A.ControlStatus = ControlStatusEnum.BUSY;
                                 AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Imbibe());
                                 AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Inject());
                                 AddReagent2.R2A.WaitStatus(AddReagent2.R2A.Rinse());
                                 AddReagent2.R2A.WaitRinseTime();

                             }

                         }

                         Transportation.TransportB.WaitStatus(Transportation.TransportB.GoIncubation());
                         Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R2_1());
                         Transportation.TransportB.WaitStatus(Transportation.TransportB.Home());
                         Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(55 - j));
                     }
                 };
             worker.RunWorkerAsync();
        }
        
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Transportation.Clear();
        }
        //单独孵育
        private void btnStripIn_Click(object sender, RoutedEventArgs e)
        {
            
            for (int i = 0; i < 2; i++)
            {
                ResetDetect();
                DeliveryWarehouse.DeliveryControl.WaitStatus(DeliveryWarehouse.DeliveryControl.CupIn());
                Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                Transportation.TransportA.WaitStatus(Transportation.TransportA.GoIncubation());
                Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(55+i));
                //TimeSpan timeSpan = DateTime.Now.AddSeconds(10)- DateTime.Now;
                //Thread.Sleep(timeSpan);
            }
        }

        //等待检测
        private void btnStripDetect_Click(object sender, RoutedEventArgs e)
        {
           
            for (int i = 0; i < 2; i++)
            { 
                ResetDetect();
                DeliveryWarehouse.DeliveryControl.WaitStatus(DeliveryWarehouse.DeliveryControl.CupIn());//进反应杯
                Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                Transportation.TransportA.WaitStatus(Transportation.TransportA.GoIncubation());
                Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(55+i));
            }
        }

        private static void ResetDetect()
        {
            Transportation.TransportA.Home();
            DetectionHouse.DetectControl.Reset();
            Transportation.TransportA.WaitStatus(0);
            DetectionHouse.DetectControl.WaitStatus(0);
            Transportation.IncubationTransport.IncubationReset();
            Transportation.IncubationTransport.WaitStatus(0);
        }
        //取出孵育
        private void button4_Click(object sender, RoutedEventArgs e)
        {
          
            for (int i = 0; i < 2; i++)
            {  
                ResetDetect();
                Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.GetStrip(55+i));
                Transportation.TransportA.WaitStatus(Transportation.TransportA.GoIncubation());

                Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                Transportation.TransportA.WaitStatus(Transportation.TransportA.Home());
            }
        }
        //检测
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            int start = int.Parse(txtPosition.Text);
            int stripCount = 1;

            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (ss, ee) =>
                {
                    //ResetDetect();
                    int order = 1;
                    //while (true)
                    //{
                        for (int j = 0; j < stripCount; j++)//检测条数
                        {
                            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.GetStrip(start - j));
                            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect_1());
                            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.GoIncubation());
                            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect());

                            for (int i = 0; i < 1; i++)//检测次数
                            {
                                DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.PositionStrip());
                                //Transportation.TransportA.Home();
                                DetectionHouse.RunEngineer();
                            }
                            //检测完之后取出
                            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.GoIncubation());
                            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect_1());
                            DetectionHouse.DetectControl.Reset();
                            DetectionHouse.DetectControl.WaitStatus(0);
                            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(start - j));//放回原来
                            //Transportation.TransportA.WaitStatus(Transportation.TransportA.GoIncubation());
                            //Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
                            //Transportation.TransportA.WaitStatus(Transportation.TransportA.Home());
                            //Thread.Sleep(60000);
                        }
                        Console.WriteLine(order);
                        order++;
                    //}
                };
            worker.RunWorkerAsync();
        }
        //反复检测
        private void button6_Click(object sender, RoutedEventArgs e)
        {
            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.Reset());
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (ss, ee) =>
                {
                    for (int i = 0; i < 150; i++)
                    {
                        DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.GoIncubation());
                        Thread.Sleep(300000);
                        DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.PositionStrip());
                        //Transportation.TransportA.Home();
                        DetectionHouse.RunEngineer();
                        Console.WriteLine(i);
                    }
                };
            worker.RunWorkerAsync();

            //无条，空着检测
            //BackgroundWorker worker = new BackgroundWorker();
            //worker.DoWork += (ss, ee) =>
            //{
            //    for (int i = 0; i < 30000; i++)
            //    {
            //        DetectionHouse.DetectControl.OnlyDetect();
            //        Thread.Sleep(1000);
            //    }
            //};
            //worker.RunWorkerAsync();
        }
        //测试剂臂
        private void button7_Click(object sender, RoutedEventArgs e)
        {
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.MoveLittle());
            //for (int i = 0; i < 2; i++)
            //{
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());
            //    WashingTank.Wash1.WashIn();
            //    Thread.Sleep(8000);
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //    WashingTank.Wash1.WashOut();
            //    WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown_1());
            //}
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.MoveLittle());
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashDown());
            //WashingTank.Wash1.WashOut();
            //WashingTank.Wash1.WaitStatus(WashingTank.Wash1.WashUp());

            //WashingTank.Wash2.WaitStatus(WashingTank.Wash2.WashDown());

            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (ss, ee) =>
                {
                    int order = 0;
                    while (true)
                    {
                        if (order % 2 == 0)
                        {
                            //试剂2A
                            AddReagent2.R2A.Imbibe();
                            AddReagent2.R2A.WaitStatus(1);
                            AddReagent2.R2A.Inject();
                            AddReagent2.R2A.WaitStatus(2);
                            AddReagent2.R2A.Rinse();
                            AddReagent2.R2A.WaitRinseTime();
                        }
                        else
                        {
                            //试剂2B
                            AddReagent2.R2B.Imbibe();
                            AddReagent2.R2B.WaitStatus(1);
                            AddReagent2.R2B.Inject();
                            AddReagent2.R2B.WaitStatus(2);
                            AddReagent2.R2B.Rinse();
                            AddReagent2.R2B.WaitRinseTime();
                        }
                        order++;
                        Console.WriteLine("R2-----------:" + order);
                    }
                };
            worker.RunWorkerAsync();
            BackgroundWorker worker2 = new BackgroundWorker();
            worker2.DoWork += (ss, ee) =>
                {
                    int order = 0;
                    while (true)
                    {
                        if (order % 2 == 0)
                        {
                            //样本臂SA
                            AddReagent1AndSample.SA.Imbibe();
                            AddReagent1AndSample.SA.WaitStatus(1);
                            AddReagent1AndSample.SA.Inject();
                            AddReagent1AndSample.SA.WaitStatus(2);
                            AddReagent1AndSample.SA.Rinse();
                            AddReagent1AndSample.SA.WaitRinseTime();
                        }
                        else
                        {
                            //样本臂SA
                            AddReagent1AndSample.SB.Imbibe();
                            AddReagent1AndSample.SB.WaitStatus(1);
                            AddReagent1AndSample.SB.Inject();
                            AddReagent1AndSample.SB.WaitStatus(2);
                            AddReagent1AndSample.SB.Rinse();
                            AddReagent1AndSample.SB.WaitRinseTime();
                        }
                        order++;
                        Console.WriteLine("S--------------:" + order);
                    }
                };
            worker2.RunWorkerAsync();
        }

        //清洗底物
        private void button8_Click(object sender, RoutedEventArgs e)
        {
            AddSubstrate.Pump.WaitStatus(AddSubstrate.Pump.PumpReset());
            AddSubstrate.Arm.WaitStatus(AddSubstrate.Arm.ArmReset());
            for (int i = 0; i < 50; i++)
            {
                AddSubstrate.Pump.WaitStatus(AddSubstrate.Pump.PumpCharge(25600));
                Thread.Sleep(2000);
                AddSubstrate.Arm.WaitStatus(AddSubstrate.Arm.ArmAddSubstrate(25600));
                AddSubstrate.Arm.ArmOut();
            }
        }
        //加底物
        private void button9_Click(object sender, RoutedEventArgs e)
        {
            DeliveryWarehouse.DeliveryControl.WaitStatus(DeliveryWarehouse.DeliveryControl.CupIn());
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.R1());
            Transportation.TransportA.WaitStatus(Transportation.TransportA.GoIncubation());
            TranAddSubstrate();
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.DropStrip(int.Parse( txtPosition.Text)));

        }
        /// <summary>
        /// 小车上有条时加底物
        /// </summary>
        private static void TranAddSubstrate()
        {
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Substrate_1());
            Transportation.TransportC.WaitStatus(Transportation.TransportC.GoIncubation());
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Substrate());
            Transportation.TransportC.WaitStatus(Transportation.TransportC.Home());
            Transportation.TransportA.Home();
            AddSubstrate.Arm.WaitStatus(AddSubstrate.Arm.ArmPosition());

            for (int i = 0; i < 8; i++)
            {
                if (i < 4)
                {
                    Transportation.TransportC.WaitStatus(Transportation.TransportC.LeftPosition());

                    AddSubstrate.Pump.WaitStatus(AddSubstrate.Pump.PumpCharge(3110));
                    AddSubstrate.Arm.WaitStatus(AddSubstrate.Arm.ArmAddSubstrate(3110));
                }
                else
                {
                    Transportation.TransportC.WaitStatus(Transportation.TransportC.RightPosition());
                    AddSubstrate.Pump.WaitStatus(AddSubstrate.Pump.PumpCharge(3110));
                    AddSubstrate.Arm.WaitStatus(AddSubstrate.Arm.ArmAddSubstrate(3110));
                }
            }
            Transportation.TransportC.WaitStatus(Transportation.TransportC.GoIncubation());
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Substrate_1());
            Transportation.TransportC.WaitStatus(Transportation.TransportC.Home());
        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            int start = int.Parse(txtPosition.Text);

            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.GetStrip(start));
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect_1());
            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.GoIncubation());
            Transportation.IncubationTransport.WaitStatus(Transportation.IncubationTransport.Detect());
            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.ThrowStrip());
            DetectionHouse.DetectControl.WaitStatus(DetectionHouse.DetectControl.Reset());
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            DetectChainHelper.DetectionOrder.GenerateCupList(ServiceProvider.WorkSheetVM);
            DetectChainHelper.DetectionOrder.StatisticsReagentKit(ServiceProvider.ReagentKitVM);
            DetectChainHelper.DetectionOrder.CheckReagent(ServiceProvider.WorkSheetVM.WorkItemList, ServiceProvider.ReagentKitVM.ReagentItemList);

            foreach (ReactionCup NewCup in DetectChainHelper.DetectionOrder.CupList)
            {
                if (NewCup.IsInStrip == false) //查看是否为新追加的项目
                {
                    ServiceProvider.gReactionCupList.Add(NewCup);
                    NewCup.IsInStrip = true;
                }
            }


            //ServiceProvider.gReactionStripList = DetectChainHelper.DetectionOrder.StripList;
            //ServiceProvider.Monitor.NeedStrips = DetectChainHelper.DetectionOrder.StripList;

            //DetectChainHelper.RunDetectChain();
        }

        private void textBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            //DeliveryCupControl.PushMeTo(Convert.ToInt32(textBox.Text)); 
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            DeliveryCupControl.DeleteCups();
            DeliveryCupControl.Init();
            DeliveryCupControl.PushMeTo(Convert.ToInt32(textBox.Text));

        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            string commandstr = "1MT" + "\r";
            Can.SendCan(86, commandstr);
        }

        private void button12_Copy_Click(object sender, RoutedEventArgs e)
        {
            string commandstr = "1ME" + "\r";
            Can.SendCan(86, commandstr);
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            IncubationBox.IncubationControl.SetReagentNeedleTemperature(Convert.ToInt32( ReagentTxt.Text));
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {         

            ArmMoveControl.detect1Variable = ArmMoveControl.detect1Variable - 20;
            this.textBox1.Text =(26300 - ArmMoveControl.detect1Variable).ToString() + "---" + ArmMoveControl.detect1Variable.ToString();
            //MessageBox.Show("更改成功！！！","提示");
        }

        private void button2_Click_1(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.detect1Variable = ArmMoveControl.detect1Variable + 20;
            this.textBox1.Text =(26300- ArmMoveControl.detect1Variable).ToString() + "----" + ArmMoveControl.detect1Variable.ToString();
            //MessageBox.Show("更改成功！！！", "提示");
        }

        private void button3_Click_1(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.detect23Variable = ArmMoveControl.detect23Variable - 20;
            this.textBox2.Text = (26300 - ArmMoveControl.detect23Variable).ToString() + "----" + ArmMoveControl.detect23Variable.ToString();
            //MessageBox.Show("更改成功！！！", "提示");
        }

        private void button6_Click_1(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.detect23Variable = ArmMoveControl.detect23Variable + 20;
            this.textBox2.Text =(26300- ArmMoveControl.detect23Variable).ToString() + "----" + ArmMoveControl.detect23Variable.ToString();
            //MessageBox.Show("更改成功！！！", "提示");
        }

        private void button7_Click_1(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.detect1Variable = ArmMoveControl.detect1Variable - 20;
            this.textBox3.Text =(26300- ArmMoveControl.detect4Variable).ToString() + "----" + ArmMoveControl.detect4Variable.ToString();
            //MessageBox.Show("更改成功！！！", "提示");
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            ArmMoveControl.detect4Variable = ArmMoveControl.detect4Variable + 20;
            this.textBox3.Text =(26300- ArmMoveControl.detect4Variable).ToString()+"----"+ ArmMoveControl.detect4Variable.ToString();
            //MessageBox.Show("更改成功！！！", "提示");
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            //textBox1.Text = ArmMoveControl.detect1Variable.ToString();
        }

        private void button8_Click_1(object sender, RoutedEventArgs e)
        {
        }

        int downCount = 0;
        private void button15_Click(object sender, RoutedEventArgs e)
        {
            if (downCount%2==0)
            {  ArmMoveControl.Z1MotorStatus = 0;
                Can.SendCan(65, "1IC15" + "\r");//开启页面探测
                Can.SendCan(65, "1IF1"+textBox4.Text + "\r");
                // string str = "1IF1" + textBox4.Text;
                
                Can.SendCan(65, "1DE500" + "\r");
                Can.SendCan(65, "1VE9.5" + "\r");
                Can.SendCan(65, "1DI-" + 27500 + "\r");
                Can.SendCan(65, "1FP" + "\r");

                ArmMoveControl.WaitStatus(65, ref ArmMoveControl.Z1MotorStatus);

                textBlock3.Text ="("+ ArmMoveControl.Z1MotorStatus+")";
                // Can.SendCan(65, "1IC16" + "\r");//关闭页面探测
                button15.Content = "上移";
            }
            else
            {
                ArmMoveControl.Z1MotorStatus = 0;
                Can.SendCan(65, "1VE9.5" + "\r");
                Can.SendCan(65, "1DI1" + "\r");
                Can.SendCan(65, "1FS1H" + "\r");
                ArmMoveControl.WaitStatus(65, ref ArmMoveControl.Z1MotorStatus);
                button15.Content = "下移";
                textBlock3.Text = "(" + ArmMoveControl.Z1MotorStatus + ")";

            }
            downCount++;
        }
    }
}
